package com.techthinker.dreamBuilder.Activitites;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.techthinker.dreamBuilder.R;
import com.techthinker.dreamBuilder.Dictionary.DictionaryAdapter;
import com.techthinker.dreamBuilder.Dictionary.DictionaryAdapter.DictionaryViewHolder;
import com.techthinker.dreamBuilder.Dictionary.DictionaryDAL;
import com.techthinker.dreamBuilder.Entity.Dictionary;

public class Dic_installedActivity extends Activity {
	private List<Dictionary> installedDics;
	private ListView list_dic;
	private DictionaryAdapter dicAdapter;
	private ImageButton ib_selectall;
	private ImageButton ib_delete;
	private DictionaryDAL dicDAL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dic_installed);

		installedDics = new ArrayList<Dictionary>();
		dicDAL = new DictionaryDAL(this);
		// add a new dictionary for header
		installedDics.add(new Dictionary());
		dicDAL.open();
		installedDics.addAll(dicDAL.fetchAllDictionarys());
		dicDAL.close();
		// add a new dictionary for bottom
		installedDics.add(new Dictionary());
		dicAdapter = new DictionaryAdapter(this, R.layout.dic_list_item,
				R.id.tv_dic_name, installedDics);
		list_dic = (ListView) findViewById(R.id.lv_installedDic);
		list_dic.setDivider(null);
		list_dic.setFocusable(false);
		list_dic.setTextFilterEnabled(true);
		list_dic.setAdapter(dicAdapter);

		ib_selectall = (ImageButton) findViewById(R.id.ib_dic_selectall);
		// change the CheckBoxes's states
		ib_selectall.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remember to refresh the views' states
				list_dic.refreshDrawableState();
				int allItem = list_dic.getChildCount() - 2;
				for (int i = 1; i <= allItem; i++) {
					DictionaryViewHolder dvh = (DictionaryViewHolder) list_dic
							.getChildAt(i).getTag();
					dvh.select.setChecked(!dvh.select.isChecked());
				}
			}
		});

		ib_delete = (ImageButton) findViewById(R.id.ib_dic_delete);
		ib_delete.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remember to refresh the views' states
				list_dic.refreshDrawableState();
				int allItem = list_dic.getChildCount() - 2;
				// open database connection
				dicDAL.open();
				for (int i = 1; i <= allItem; i++) {
					DictionaryViewHolder dvh = (DictionaryViewHolder) list_dic
							.getChildAt(i).getTag();
					Dictionary dic = (Dictionary) list_dic.getItemAtPosition(i);
					if (dvh.select.isChecked()) {
						//delete the dictionary and it's related lists, words
						dicDAL.deleteDictionary(dic);
					}
				}
				installedDics = new ArrayList<Dictionary>();
				installedDics.add(new Dictionary());
				installedDics.addAll(dicDAL.fetchAllDictionarys());
				installedDics.add(new Dictionary());
				dicAdapter.clear();
				for(Dictionary d:installedDics ){
					dicAdapter.add(d);
				}
				dicAdapter.NUM_DICTIONARY=dicAdapter.getCount()-2;
				dicAdapter.notifyDataSetChanged();
				dicDAL.close();
				// close database connection
				dicAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		dicDAL.open();
		installedDics = new ArrayList<Dictionary>();
		installedDics.add(new Dictionary());
		installedDics.addAll(dicDAL.fetchAllDictionarys());
		installedDics.add(new Dictionary());
		dicAdapter.clear();
		for(Dictionary d:installedDics ){
			dicAdapter.add(d);
		}
		dicAdapter.NUM_DICTIONARY=dicAdapter.getCount()-2;
		dicAdapter.notifyDataSetChanged();
		dicDAL.close();
	}
}
